package com.example.huichaoliu;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.huichaoliu.app.R;
import com.huichaoliu.data.Config;
import com.rederxu.farmer.BaseActivity;
import com.rederxu.request.HTTPRequest;
import com.rederxu.request.onRequestOverListener;
import com.rederxu.tools.StringUtils;
import com.source.utils.Utils;

public class PersonalInfoActivity extends BaseActivity implements OnClickListener {

	private EditText et_height = null;
	private EditText et_weight = null;
	private EditText et_waist = null;
	private EditText et_jacket = null;
	private EditText et_bust = null;
	private EditText et_shoes = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_personal_info);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.left_button:
			finish();
			break;
		case R.id.tv_fix_personal_info:
			startActivity(PersonalDataActivity.class);
			break;
		case R.id.btn_go:
			
			int height = 0;
			try{
				height = Integer.parseInt(et_height.getText().toString());
				if(height < 100 || height > 250){
					showToast("请输入正确的身高（cm）");
					return;
				}
			}catch(Exception e){
				e.printStackTrace();
				showToast("请输入正确的身高（cm）");
				return;
			}
			
			int waist = 0;
			try{
				waist = Integer.parseInt(et_waist.getText().toString());
			}catch(Exception e){
				e.printStackTrace();
				showToast("请输入正确的腰围");
				return;
			}

			
			int weight = 0;
			try{
				weight = Integer.parseInt(et_weight.getText().toString());
				if(weight < 20 || weight > 200){
					showToast("请输入正确的体重（kg）");
					return;
				}
			}catch(Exception e){
				e.printStackTrace();
				showToast("请输入正确的体重（kg）");
				return;
			}
			
			String jacksize = et_jacket.getText().toString();
			if(StringUtils.isBlank(jacksize)){
				showToast("请输入正确的上衣大小");
				return;
			}
			
			int bust = 0;
			try{
				bust = Integer.parseInt(et_bust.getText().toString());
			}catch(Exception e){
				e.printStackTrace();
				showToast("请输入正确的胸围");
				return;
			}
			
			int shoes = 0;
			try{
				shoes = Integer.parseInt(et_shoes.getText().toString());
			}catch(Exception e){
				e.printStackTrace();
				showToast("请输入正确的鞋码");
				return;
			}
			
			try{
				JSONObject jo = new JSONObject();
				jo.put("userId", Config.getUserId(this));
				jo.put("height", height);
				jo.put("weight", weight);
				jo.put("footsize", shoes);
				jo.put("waistsize", waist);
				jo.put("jacketsize", jacksize);
				jo.put("bust", bust);
				Bundle bundle = new Bundle();
				bundle.putString("request", Utils.requestParamsEncode(jo));
				HTTPRequest.POST(this, Config.REQUEST_URLS.URL_USER_ADD_MODEL_INFO, bundle, new onRequestOverListener() {
					@Override
					public void onRequestOver(String result) {
						Log.e("result", result);
						try{
							JSONObject jo = new JSONObject(result);
							if(jo.getInt("code") != 0){
								showToast(jo.getString("message"));
								return;
							}
							jo = jo.getJSONObject("response");
						}catch(JSONException e){
							e.printStackTrace();
						}
					}
				});
			}catch(Exception e){
				e.printStackTrace();
			}
			break;
		}
	}

	@Override
	public void init() {
		((TextView) findViewById(R.id.title_text)).setText("个人信息");
		findViewById(R.id.left_button).setOnClickListener(this);
		findViewById(R.id.btn_go).setOnClickListener(this);
		findViewById(R.id.tv_fix_personal_info).setOnClickListener(this);

		et_height = (EditText) findViewById(R.id.et_height);
		et_weight = (EditText) findViewById(R.id.et_weight);
		et_waist = (EditText) findViewById(R.id.et_waist);
		et_jacket = (EditText) findViewById(R.id.et_jack);
		et_bust = (EditText) findViewById(R.id.et_bust);
		et_shoes = (EditText) findViewById(R.id.et_shoes);
	}

	@Override
	public void initData() {
		try{
			JSONObject jo = new JSONObject();
			jo.put("userId", Config.getUserId(mContext));
			Bundle bundle = new Bundle();
			bundle.putString("request", Utils.requestParamsEncode(jo));
			HTTPRequest.POST(this, Config.REQUEST_URLS.URL_USER_GET_MODEL_INFO, bundle, new onRequestOverListener() {
				@Override
				public void onRequestOver(String result) {
					Log.e("result", result);
					try{
						JSONObject jo = new JSONObject(result);
						if(jo.getInt("code") != 0){
							return;
						}
						jo = jo.getJSONObject("response");
					}catch(JSONException e){
						e.printStackTrace();
					}
				}
			});
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public void onNetworkChanged(boolean isMobNetConnected,
			boolean isWifiConnected) {
		// TODO Auto-generated method stub

	}

}
